package com.maxd.controller;

import com.maxd.adapter.LoginUserHolder;
import com.maxd.bean.PageRes;
import com.maxd.error.ExceptionEnum;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.ClassInfo;
import com.maxd.model.ClassStudentInfo;
import com.maxd.model.StudentInfo;
import com.maxd.respository.ClassInfoRepository;
import com.maxd.service.ClassService;
import com.maxd.service.ClassStudentService;
import com.maxd.utils.AdminUtils;
import com.maxd.utils.AssertUtil;
import com.maxd.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Api(value="班级学员管理", tags = {"班级学员管理"})
@RequestMapping(value = "/class-student")
public class ClassStudentController {

    @Autowired
    private ClassStudentService classStudentService;

    @Autowired
    private ClassInfoRepository classInfoRepository;

    @PostMapping("/add-student")
    @ApiOperation("内管班级添加学员")
    public Result<Void> addStudent(@RequestBody @Validated ClassStudentInfo classStudentInfo) {
        AdminUtils.checkLogin();
        classStudentService.addStudent(classStudentInfo);
        return ResultUtil.success(null);
    }

    @GetMapping("/class-student-list")
    @ApiOperation("内管查询班级学员列表")
    public Result<List<ClassStudentInfo>> studentList(@ApiParam("班级ID") @RequestParam(value = "classId") Long classId,
                                                 @ApiParam("查询生效记录 1-查询生效的记录 其他查询所有记录") @RequestParam(value = "effect", required = false) String effect) {
        List<ClassStudentInfo> studentInfos = classStudentService.queryList(classId, effect, null);
        return ResultUtil.success(studentInfos);
    }

    @GetMapping("/my-class-student")
    @ApiOperation("商城端教官查询班级学生列表")
    public Result<List<ClassStudentInfo>> myStudentList(@ApiParam("班级ID") @RequestParam(value = "classId") Long classId) {
        Long userId = LoginUserHolder.getUserId();
        AssertUtil.assertTrue(userId != null && userId > 0, ExceptionEnum.WRONT_TOKEN);

        ClassInfo classInfo = classInfoRepository.findById(classId).orElse(null);
        AssertUtil.notNull(classInfo, ExceptionEnum.CLASS_NOF);
        AssertUtil.assertTrue(classInfo.getInstructorUserId().equals(userId) || classInfo.getAssistantUserId().equals(userId), ExceptionEnum.PRIVILEGE_ERROR);

        List<ClassStudentInfo> studentInfos = classStudentService.queryList(classId, null, null);
        return ResultUtil.success(studentInfos);
    }

}
